<div class="table-responsive">
    <table class="table table-bordered table-hover" >
        <thead>
        <tr>

            <td class="text-left">商品名称</td>
            <td class="text-left">价格</td>
            <td class="text-left">库存</td>
            <td class="text-left">
                <input type="checkbox" class="allSelect" onclick="$('input.input-sm.goods-quantity').prop('checked', this.checked);">选择
            </td>
            <td class="text-left">操作</td>
        </tr>
        </thead>
        <tbody id="goos_table">
        <volist name="goodsList" id="list">
            <tr>

                <td class="text-left">{$list.goods_name|getSubstr=0,33} {$list.huodong} (&nbsp;{$list.key_name}&nbsp;)
                </td>
                <td class="text-left">{$list.price}</td>
                <td class="text-left">{$list.store_count}</td>
                <td class="text-left">
                    <input type="checkbox" name="goods_id[{$list.goods_id}][{$list.key}][goods_num]" value="1"
                           class="input-sm goods-quantity" key="{$list.key}" onkeyup="this.value=this.value.replace(/[^\d]/g,'')"
                           onpaste="this.value=this.value.replace(/[^\d]/g,'')"/>
                </td>
                <td><a href="javascript:void(0)" onclick="javascript:$(this).parent().parent().remove();">删除</a></td>
            </tr>
            </if>
        </volist>

        </tbody>
        <tfoot>
        <tr>
            <td class="text-right" colspan="5">
                <a href="javascript:void(0)" onclick="select_goods();" class="btn btn-info">确定</a>
            </td>
        </tr>
        </tfoot>
    </table>
</div>
<div class="row">
    <div class="text-left col-sm-10">
        {$page}
    </div>
    <div class="text-right col-sm-2">

    </div>
</div>
<script>

    // 存储选中的用户ID - 改为从父页面获取
    var selectedUserIds = window.parent.selectedUserIds || [];
    // console.log('改为从父页面获取：' + selectedUserIds)

    // 全选/取消全选
    $('input.allSelect').click(function(){
        // console.log('点击了全选');
        var isChecked = $(this).prop('checked');
        $('input.input-sm.goods-quantity').prop('checked', isChecked).trigger('change');
    });

    // 单个复选框变化时更新数组
    $(document).on('change', 'input.input-sm.goods-quantity', function(){
        // console.log('单个复选框变化时更新数组');
        var goods_id = $(this).attr('key');
        if($(this).is(':checked')) {
            // console.log('选中了：' + goods_id + ' ，添加到数组中');
            if(selectedUserIds.indexOf(goods_id) === -1) {
                selectedUserIds.push(goods_id);
            }
        } else {
            selectedUserIds = selectedUserIds.filter(function(id) {
                return id !== goods_id;
            });
        }
        // 更新父页面的选中数组
        window.parent.selectedUserIds = selectedUserIds;
        // console.log('更新父页面的选中数组：' + selectedUserIds);
    });

    // 删除行时同时从数组中移除ID
    $(document).on('click', 'a[onclick*="remove"]', function(){
        // console.log('删除行时同时从数组中移除ID');
        var goods_id = $(this).closest('tr').find('input.input-sm.goods-quantity').attr('key');
        selectedUserIds = selectedUserIds.filter(function(id) {
            return id !== goods_id;
        });
        window.parent.selectedUserIds = selectedUserIds;
        $(this).closest('tr').remove();
    });

    function select_goods_() {

        if ($("input[type='checkbox']:checked").length == 0) {
            layer.alert('请选择商品', {icon: 2}); //alert('请选择商品');
            return false;
        }

        // 将没选中的复选框所在的  tr  remove  然后删除复选框
        $("input[type='checkbox']").each(function () {
            if ($(this).is(':checked') == false) {
                $(this).parent().parent().remove();
            }
            // $(this).siblings().show();
            // $(this).remove();
        });

        // $(".btn-info").remove();
        //先获取到表格的HTML
        var tableHtml = $('#goos_table').html();
        console.log(tableHtml);
        //将checkbox的type 替换成text
        tableHtml = tableHtml.replace(/type="checkbox"/g, 'type="text"');
        // console.log(tableHtml);
        // 调用父页面的方法，将表格的HTML传递给父页面
        javascript:window.parent.call_back(tableHtml);
        // javascript:window.parent.call_back($('#goos_table').html().replace(/选择/, '购买数量'));
    }
    function select_goods() {



        if (selectedUserIds.length == 0) {
            layer.alert('请选择商品', {icon: 2}); //alert('请选择商品');
            return false;
        }

        //通过接口构建表格
        $.ajax({
            type: "POST",
            url: "/index.php/Seller/Order/ajax_order_goods_table",
            data: {goods_ids: selectedUserIds.join(',')},
            success: function (data) {
                window.parent.selectedUserIds = [];//清空数组
                javascript:window.parent.call_back(data);
            }
        });

    }

    $(".pagination  a").click(function(){
        var page = $(this).data('p');
        ajax_get_table('search-form2',page);
    });



</script>